
##################################################################
##################################################################
## Replication Material
##
## Widmann & Simonsen: 
## Setting the Tone: The Diffusion of Moral and Moral-Emotional Appeals 
## across Political and Public Discourse
##
## Political Science Research & Methods (PSRM)
##
##
##################################################################
##################################################################

# Note: The file 000_readme.pdf describes all scripts and datasets required to replicate the analysis

# This script was run on the following R version, platform and OS:
# R version 4.4.0 (2024-04-24)
# Platform: aarch64-apple-darwin20
# Running under: macOS Sonoma 14.3.1


# Packages --------------------------------------------------

library(tidyverse)
library(vars)
library(gridExtra)
library(openxlsx)
library(lubridate)
library(academictwitteR)
library(rdrobust)
library(tseries)
library(cowplot)
library(svars)

# Working directory ----------------------------------------------
# We recommend setting the working directory to the downloaded replication folder
setwd("./files")

# Load prepared data ----------------------------------------------

load("./public_tweets.Rdata")
load("./politics_tweets.Rdata")
load("./newspaper_tweets.Rdata")

# Analysis -----------------------------------------------------


### Figure 1 ----------------------------------


results1_svar <- svar_simple("morality_only_pos", 1)

coef_plot_svar1 <- create_coef_plot_simple(results1_svar)

results2_svar <- svar_simple("morality_only_neg", 1)

coef_plot_svar2 <- create_coef_plot_simple(results2_svar)

results3_svar <- svar_simple("pos_moremo", 1)

coef_plot_svar3 <- create_coef_plot_simple(results3_svar)

results4_svar <- svar_simple("neg_moremo", 1)

coef_plot_svar4 <- create_coef_plot_simple(results4_svar)



# Combine the two plots
plot_simple_svar_p1 <- create_coef_plot_simple2(results1_svar, results2_svar, results3_svar, results4_svar)
ggsave("./figures/figure1.pdf", width=12, height=8)


### Figure 2 ---------------------------------------------------

emotional_appeals <- c("morality_only_pos", "morality_only_neg", "pos_moremo", "neg_moremo")

complex_complete <- NULL
results <- svar_complex_complete_binary(emotional_appeals, 1)
create_coef_plot_complex_complete_binary(results)
ggsave("./figures/figure2.pdf", width=12, height=8)


### Table C1 --------------------------------------------------


# Define model names and their new descriptions
model_names <- c("morality_only_pos", "morality_only_neg", "neg_moremo", "pos_moremo")
new_names <- c("Positive Moral", "Negative Moral", "Negative Moral-Emotional", "Positive Moral-Emotional")

# Apply the function to each model and store results
results_df <- lapply(model_names, get_aic_sc) %>%
  bind_rows() %>%
  mutate(Model = new_names) 

# Rearrange columns to place 'Model' at the front
results_df <- results_df %>%
  dplyr::select(Model, AIC, SC)

write.xlsx(results_df, file = "./tables/tableC1.xlsx")

### Table C2 --------------------------------------------------



# Define model names and their new descriptions
model_names <- c("morality_only_pos", "morality_only_neg", "neg_moremo", "pos_moremo")
new_names <- c("Positive Moral", "Negative Moral", "Negative Moral-Emotional", "Positive Moral-Emotional")

# Apply the function to each model and store results
results_df <- lapply(model_names, get_aic_sc_complex) %>%
  bind_rows() %>%
  mutate(Model = new_names) 

# Rearrange columns to place 'Model' at the front
results_df <- results_df %>%
  dplyr::select(Model, AIC, SC)

write.xlsx(results_df, file = "./tables/tableC2.xlsx")


### Figure D1 ------------------------------------------

newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
newspaper_tweets_second <- newspaper_tweets |> 
  filter(date >= "2017-09-25")

politics_second <- politics |> 
  filter(date >= "2017-09-25")

polinterest$date <- as.Date(polinterest$created_at)
polinterest_second <- polinterest |> 
  filter(date >= "2017-09-25")

partytype_agg <- aggregate(politics_second$morality_only_pos, list(politics_second$date, politics_second$partytype), mean)
colnames(partytype_agg)[1] <- "date"
colnames(partytype_agg)[2] <- "partytype"
partytype_wide <- partytype_agg %>% spread(partytype, x)

newspaper_tweets_agg <- aggregate(newspaper_tweets_second$morality_only_pos, list(newspaper_tweets_second$date), mean)
colnames(newspaper_tweets_agg)[1] <- "date"
colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"

polinterest_agg <- aggregate(polinterest_second$morality_only_pos, list(polinterest_second$date), mean)
colnames(polinterest_agg)[1] <- "date"
colnames(polinterest_agg)[2] <- "polinterest_morality"

# Create a sequence of dates from August 15, 2015 to January 01, 2017
date_sequence <- seq(from = as.Date("2017-09-25"), to = as.Date("2022-01-01"), by = "day")

# Create a dataframe using the date sequence
var_complete <- tibble(date = date_sequence)

var_complete <- merge(var_complete, partytype_wide, all.x = TRUE)
var_complete <- merge(var_complete, newspaper_tweets_agg, all.x = TRUE)
var_complete <- merge(var_complete, polinterest_agg, all.x = TRUE)

#var_partytype <- var_partytype |>
#filter(date > "2015-07-01" & date < "2016-08-01")

#set NAs to 0
#var_complete[is.na(var_complete)] <- 0
var_complete$date <- NULL


# Calculate the mean of each numeric column
means <- sapply(var_complete, function(x) mean(x, na.rm = TRUE))

# Create a data frame suitable for ggplot
mean_data <- data.frame(
  category = names(means),
  mean_value = means
)

# Convert the category column to character
mean_data$category <- as.character(mean_data$category)


# Rename categories (replace 'oldname1' and 'oldname2' with current names, 'newname1' and 'newname2' with desired new names)
mean_data$category[mean_data$category == 'newspaper_tweets_morality'] <- 'newspapers'
mean_data$category[mean_data$category == 'polinterest_morality'] <- 'public'

mean_data <- mean_data |> 
  filter(category != "newspapers")

# Desired order for the categories on the x-axis
desired_order <- c("newspapers", "public", "radicalleft", "centerleft", "centerright", "radicalright")
# Reorder the categories based on the desired order
mean_data$category <- factor(mean_data$category, levels = desired_order)

# Create the barplot without legend and with the viridis color scheme
ggplot(mean_data, aes(x = category, y = mean_value, fill = category)) +
  geom_bar(stat = "identity") +
  scale_fill_grey() + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(
    title = "Mean Values of Different Groups",
    x = "Category",
    y = "Mean Value"
  ) +
  guides(fill = FALSE) +
  theme_bw()
ggsave("./figures/figureD1.pdf", width=12, height=8)

### Figure D2 -----------------------------------------------

partytype_agg <- aggregate(politics_second$morality_only_neg, list(politics_second$date, politics_second$partytype), mean)
colnames(partytype_agg)[1] <- "date"
colnames(partytype_agg)[2] <- "partytype"
partytype_wide <- partytype_agg %>% spread(partytype, x)

newspaper_tweets_agg <- aggregate(newspaper_tweets_second$morality_only_neg, list(newspaper_tweets_second$date), mean)
colnames(newspaper_tweets_agg)[1] <- "date"
colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"

polinterest_agg <- aggregate(polinterest_second$morality_only_neg, list(polinterest_second$date), mean)
colnames(polinterest_agg)[1] <- "date"
colnames(polinterest_agg)[2] <- "polinterest_morality"

# Create a sequence of dates from August 15, 2015 to January 01, 2017
date_sequence <- seq(from = as.Date("2017-09-25"), to = as.Date("2022-01-01"), by = "day")

# Create a dataframe using the date sequence
var_complete <- tibble(date = date_sequence)

var_complete <- merge(var_complete, partytype_wide, all.x = TRUE)
var_complete <- merge(var_complete, newspaper_tweets_agg, all.x = TRUE)
var_complete <- merge(var_complete, polinterest_agg, all.x = TRUE)

#var_partytype <- var_partytype |>
#filter(date > "2015-07-01" & date < "2016-08-01")

#set NAs to 0
#var_complete[is.na(var_complete)] <- 0
var_complete$date <- NULL


# Calculate the mean of each numeric column
means <- sapply(var_complete, function(x) mean(x, na.rm = TRUE))

# Create a data frame suitable for ggplot
mean_data <- data.frame(
  category = names(means),
  mean_value = means
)

# Convert the category column to character
mean_data$category <- as.character(mean_data$category)


# Rename categories (replace 'oldname1' and 'oldname2' with current names, 'newname1' and 'newname2' with desired new names)
mean_data$category[mean_data$category == 'newspaper_tweets_morality'] <- 'newspapers'
mean_data$category[mean_data$category == 'polinterest_morality'] <- 'public'

mean_data <- mean_data |> 
  filter(category != "newspapers")

# Desired order for the categories on the x-axis
desired_order <- c("newspapers", "public", "radicalleft", "centerleft", "centerright", "radicalright")
# Reorder the categories based on the desired order
mean_data$category <- factor(mean_data$category, levels = desired_order)

# Create the barplot without legend and with the viridis color scheme
ggplot(mean_data, aes(x = category, y = mean_value, fill = category)) +
  geom_bar(stat = "identity") +
  scale_fill_grey() + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(
    title = "Mean Values of Different Groups",
    x = "Category",
    y = "Mean Value"
  ) +
  guides(fill = FALSE) +
  theme_bw()
ggsave("./figures/figureD2.pdf", width=12, height=8)

### Figure D3 -----------------------------------------------

partytype_agg <- aggregate(politics_second$pos_moremo, list(politics_second$date, politics_second$partytype), mean)
colnames(partytype_agg)[1] <- "date"
colnames(partytype_agg)[2] <- "partytype"
partytype_wide <- partytype_agg %>% spread(partytype, x)

newspaper_tweets_agg <- aggregate(newspaper_tweets_second$pos_moremo, list(newspaper_tweets_second$date), mean)
colnames(newspaper_tweets_agg)[1] <- "date"
colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"

polinterest_agg <- aggregate(polinterest_second$pos_moremo, list(polinterest_second$date), mean)
colnames(polinterest_agg)[1] <- "date"
colnames(polinterest_agg)[2] <- "polinterest_morality"

# Create a sequence of dates from August 15, 2015 to January 01, 2017
date_sequence <- seq(from = as.Date("2017-09-25"), to = as.Date("2022-01-01"), by = "day")

# Create a dataframe using the date sequence
var_complete <- tibble(date = date_sequence)

var_complete <- merge(var_complete, partytype_wide, all.x = TRUE)
var_complete <- merge(var_complete, newspaper_tweets_agg, all.x = TRUE)
var_complete <- merge(var_complete, polinterest_agg, all.x = TRUE)

#var_partytype <- var_partytype |>
#filter(date > "2015-07-01" & date < "2016-08-01")

#set NAs to 0
#var_complete[is.na(var_complete)] <- 0
var_complete$date <- NULL


# Calculate the mean of each numeric column
means <- sapply(var_complete, function(x) mean(x, na.rm = TRUE))

# Create a data frame suitable for ggplot
mean_data <- data.frame(
  category = names(means),
  mean_value = means
)

# Convert the category column to character
mean_data$category <- as.character(mean_data$category)


# Rename categories (replace 'oldname1' and 'oldname2' with current names, 'newname1' and 'newname2' with desired new names)
mean_data$category[mean_data$category == 'newspaper_tweets_morality'] <- 'newspapers'
mean_data$category[mean_data$category == 'polinterest_morality'] <- 'public'

mean_data <- mean_data |> 
  filter(category != "newspapers")

# Desired order for the categories on the x-axis
desired_order <- c("newspapers", "public", "radicalleft", "centerleft", "centerright", "radicalright")
# Reorder the categories based on the desired order
mean_data$category <- factor(mean_data$category, levels = desired_order)

# Create the barplot without legend and with the viridis color scheme
ggplot(mean_data, aes(x = category, y = mean_value, fill = category)) +
  geom_bar(stat = "identity") +
  scale_fill_grey() + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(
    title = "Mean Values of Different Groups",
    x = "Category",
    y = "Mean Value"
  ) +
  guides(fill = FALSE) +
  theme_bw()
ggsave("./figures/figureD3.pdf", width=12, height=8)

### Figure D4 -----------------------------------------------

partytype_agg <- aggregate(politics_second$neg_moremo, list(politics_second$date, politics_second$partytype), mean)
colnames(partytype_agg)[1] <- "date"
colnames(partytype_agg)[2] <- "partytype"
partytype_wide <- partytype_agg %>% spread(partytype, x)

newspaper_tweets_agg <- aggregate(newspaper_tweets_second$neg_moremo, list(newspaper_tweets_second$date), mean)
colnames(newspaper_tweets_agg)[1] <- "date"
colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"

polinterest_agg <- aggregate(polinterest_second$neg_moremo, list(polinterest_second$date), mean)
colnames(polinterest_agg)[1] <- "date"
colnames(polinterest_agg)[2] <- "polinterest_morality"

# Create a sequence of dates from August 15, 2015 to January 01, 2017
date_sequence <- seq(from = as.Date("2017-09-25"), to = as.Date("2022-01-01"), by = "day")

# Create a dataframe using the date sequence
var_complete <- tibble(date = date_sequence)

var_complete <- merge(var_complete, partytype_wide, all.x = TRUE)
var_complete <- merge(var_complete, newspaper_tweets_agg, all.x = TRUE)
var_complete <- merge(var_complete, polinterest_agg, all.x = TRUE)

#var_partytype <- var_partytype |>
#filter(date > "2015-07-01" & date < "2016-08-01")

#set NAs to 0
#var_complete[is.na(var_complete)] <- 0
var_complete$date <- NULL


# Calculate the mean of each numeric column
means <- sapply(var_complete, function(x) mean(x, na.rm = TRUE))

# Create a data frame suitable for ggplot
mean_data <- data.frame(
  category = names(means),
  mean_value = means
)

# Convert the category column to character
mean_data$category <- as.character(mean_data$category)


# Rename categories (replace 'oldname1' and 'oldname2' with current names, 'newname1' and 'newname2' with desired new names)
mean_data$category[mean_data$category == 'newspaper_tweets_morality'] <- 'newspapers'
mean_data$category[mean_data$category == 'polinterest_morality'] <- 'public'

mean_data <- mean_data |> 
  filter(category != "newspapers")

# Desired order for the categories on the x-axis
desired_order <- c("newspapers", "public", "radicalleft", "centerleft", "centerright", "radicalright")
# Reorder the categories based on the desired order
mean_data$category <- factor(mean_data$category, levels = desired_order)

# Create the barplot without legend and with the viridis color scheme
ggplot(mean_data, aes(x = category, y = mean_value, fill = category)) +
  geom_bar(stat = "identity") +
  scale_fill_grey() + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(
    title = "Mean Values of Different Groups",
    x = "Category",
    y = "Mean Value"
  ) +
  guides(fill = FALSE) +
  theme_bw()
ggsave("./figures/figureD4.pdf", width=12, height=8)


### Table D1 ----------------------------------------

emotional_appeals <- c("morality_only_pos", "morality_only_neg", "pos_moremo", "neg_moremo")

# Initialize an empty data frame to store results
all_results <- tibble()

# Loop through each emotional appeal and combine results
for (appeal in emotional_appeals) {
  appeal_stats <- calculate_stats_simple(appeal)
  all_results <- bind_rows(all_results, appeal_stats)
}

# Rename columns
all_results <- all_results %>%
  rename(
    Mean_Politics = mean_politics_morality,
    SD_Politics = sd_politics_morality,
    Mean_Public = mean_public_tweets_morality,
    SD_Public = sd_public_tweets_morality
  ) %>%
  mutate(emotional_appeal = new_names)

write.xlsx(all_results, file = "./tables/tableD1.xlsx")

### Figure D5 -----------------------------------

create_histograms_politics()
# Save to file
ggsave("./figures/figureD5.pdf", width=12, height=8)

### Figure D6 -----------------------------------


create_histograms_public()
ggsave("./figures/figureD6.pdf", width=12, height=8)


### Figure E1 -----------------------------------

results1_svar <- svar_simple("morality_only_pos", 4)

coef_plot_svar1 <- create_coef_plot_simple(results1_svar)

results2_svar <- svar_simple("morality_only_neg", 4)

coef_plot_svar2 <- create_coef_plot_simple(results2_svar)

results3_svar <- svar_simple("pos_moremo", 4)

coef_plot_svar3 <- create_coef_plot_simple(results3_svar)

results4_svar <- svar_simple("neg_moremo", 4)

coef_plot_svar4 <- create_coef_plot_simple(results4_svar)

# Combine the four plots
plot_simple_svar_p4 <- create_coef_plot_simple2(results1_svar, results2_svar, results3_svar, results4_svar)
ggsave("./figures/figureE1.pdf", width=12, height=8)


### Figure E2 -----------------------------------


emotional_appeals <- c("morality_only_pos", "morality_only_neg", "pos_moremo", "neg_moremo")

complex_complete <- NULL
results <- svar_complex_complete_binary(emotional_appeals, 4)
create_coef_plot_complex_complete_binary(results)
ggsave("./figures/figureE2.pdf", width=12, height=8)


### Table F1 ------------------------------------

adf_results <- run_adf_tests_simple()
print(adf_results)

write.xlsx(adf_results, file = "./tables/tableF1.xlsx")

### Table F2 ------------------------------------

adf_results_complex <- run_adf_tests_complex()
print(adf_results_complex)

write.xlsx(adf_results_complex, file = "./tables/tableF2.xlsx")


### Figure F1 -----------------------------------


plot1 <- get_residual_plots_simple("morality_only_pos", 1)
plot2 <- get_residual_plots_simple("morality_only_neg", 1)
plot3 <- get_residual_plots_simple("pos_moremo", 1)
plot4 <- get_residual_plots_simple("neg_moremo", 1)
plot <- plot_grid(plot1, plot2, plot3, plot4, ncol = 2, rel_heights = 4)
ggsave("./figures/figureF1.pdf", width=12, height=8)



### Figure F2 -----------------------------------

plot1 <- get_residual_plots_complex("morality_only_pos", 1)
plot2 <- get_residual_plots_complex("morality_only_neg", 1)
plot3 <- get_residual_plots_complex("pos_moremo", 1)
plot4 <- get_residual_plots_complex("neg_moremo", 1)
plot <- plot_grid(plot1, plot2, plot3, plot4, ncol = 2, rel_heights = 4)
ggsave("./figures/figureF2.pdf", width=12, height=8)

### Figure F3 ------------------------------------


newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
newspaper_tweets_second <- newspaper_tweets |> 
  filter(date >= "2017-09-25")

politics_second <- politics |> 
  filter(date >= "2017-09-25")

polinterest$date <- as.Date(polinterest$created_at)
polinterest_second <- polinterest |> 
  filter(date >= "2017-09-25")

newspaper_tweets_agg <- aggregate(newspaper_tweets_second$morality_only_pos, list(newspaper_tweets_second$date), mean)
colnames(newspaper_tweets_agg)[1] <- "date"
colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"

politics_agg <- aggregate(politics_second$morality_only_pos, list(politics_second$date), mean)
colnames(politics_agg)[1] <- "date"
colnames(politics_agg)[2] <- "politics_morality"

polinterest_agg <- aggregate(polinterest_second$morality_only_pos, list(polinterest_second$date), mean)
colnames(polinterest_agg)[1] <- "date"
colnames(polinterest_agg)[2] <- "polinterest_morality"

newspaper_tweets_agg <- newspaper_tweets_agg |>
  filter(date > "2017-09-24" & date < "2022-01-01")

politics_agg <- politics_agg |>
  filter(date > "2017-09-24" & date < "2022-01-01")

polinterest_agg <- polinterest_agg |>
  filter(date > "2017-09-24" & date < "2022-01-01")

# Create a sequence of dates from August 15, 2015 to January 01, 2017
date_sequence <- seq(from = as.Date("2017-09-24"), to = as.Date("2022-01-01"), by = "day")

# Create a dataframe using the date sequence
var_dataframe <- tibble(date = date_sequence)

var_dataframe <- merge(var_dataframe, newspaper_tweets_agg, all.x = TRUE)
var_dataframe <- merge(var_dataframe, politics_agg, all.x = TRUE)
var_dataframe <- merge(var_dataframe, polinterest_agg, all.x = TRUE)

#set NAs to 0
var_dataframe[is.na(var_dataframe)] <- 0
var_dataframe$date <- NULL

var <- VAR(var_dataframe, p = 1, type = "const")


# Open the PNG device
pdf("./figures/figureF3.pdf", width = 12, height = 4)

# Set up the layout to have 3 plots in a row
par(mfrow=c(1, 3))

# Plot 1: Series Newspaper
acf(residuals(var)[,1], main="Series Newspaper")

# Plot 2: Series Politics
acf(residuals(var)[,2], main="Series Politics")

# Plot 3: Series Public
acf(residuals(var)[,3], main="Series Public")

# Reset the par settings to default (optional, useful if you're continuing with other plots)
par(mfrow=c(1, 1))

# Close the device
dev.off()


### Figure F4 ---------------------------------------------

newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
newspaper_tweets_second <- newspaper_tweets |> 
  filter(date >= "2017-09-25")

politics_second <- politics |> 
  filter(date >= "2017-09-25")

polinterest$date <- as.Date(polinterest$created_at)
polinterest_second <- polinterest |> 
  filter(date >= "2017-09-25")

newspaper_tweets_agg <- aggregate(newspaper_tweets_second$morality_only_neg, list(newspaper_tweets_second$date), mean)
colnames(newspaper_tweets_agg)[1] <- "date"
colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"

politics_agg <- aggregate(politics_second$morality_only_neg, list(politics_second$date), mean)
colnames(politics_agg)[1] <- "date"
colnames(politics_agg)[2] <- "politics_morality"

polinterest_agg <- aggregate(polinterest_second$morality_only_neg, list(polinterest_second$date), mean)
colnames(polinterest_agg)[1] <- "date"
colnames(polinterest_agg)[2] <- "polinterest_morality"

newspaper_tweets_agg <- newspaper_tweets_agg |>
  filter(date > "2017-09-24" & date < "2022-01-01")

politics_agg <- politics_agg |>
  filter(date > "2017-09-24" & date < "2022-01-01")

polinterest_agg <- polinterest_agg |>
  filter(date > "2017-09-24" & date < "2022-01-01")

# Create a sequence of dates from August 15, 2015 to January 01, 2017
date_sequence <- seq(from = as.Date("2017-09-24"), to = as.Date("2022-01-01"), by = "day")

# Create a dataframe using the date sequence
var_dataframe <- tibble(date = date_sequence)

var_dataframe <- merge(var_dataframe, newspaper_tweets_agg, all.x = TRUE)
var_dataframe <- merge(var_dataframe, politics_agg, all.x = TRUE)
var_dataframe <- merge(var_dataframe, polinterest_agg, all.x = TRUE)

#set NAs to 0
var_dataframe[is.na(var_dataframe)] <- 0
var_dataframe$date <- NULL

var <- VAR(var_dataframe, p = 1, type = "const")


# Open the PNG device
pdf("./figures/figureF4.pdf", width = 12, height = 4)

# Set up the layout to have 3 plots in a row
par(mfrow=c(1, 3))

# Plot 1: Series Newspaper
acf(residuals(var)[,1], main="Series Newspaper")

# Plot 2: Series Politics
acf(residuals(var)[,2], main="Series Politics")

# Plot 3: Series Public
acf(residuals(var)[,3], main="Series Public")

# Reset the par settings to default (optional, useful if you're continuing with other plots)
par(mfrow=c(1, 1))

# Close the device
dev.off()

### Figure F5 ---------------------------------------------

newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
newspaper_tweets_second <- newspaper_tweets |> 
  filter(date >= "2017-09-25")

politics_second <- politics |> 
  filter(date >= "2017-09-25")

polinterest$date <- as.Date(polinterest$created_at)
polinterest_second <- polinterest |> 
  filter(date >= "2017-09-25")

newspaper_tweets_agg <- aggregate(newspaper_tweets_second$pos_moremo, list(newspaper_tweets_second$date), mean)
colnames(newspaper_tweets_agg)[1] <- "date"
colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"

politics_agg <- aggregate(politics_second$pos_moremo, list(politics_second$date), mean)
colnames(politics_agg)[1] <- "date"
colnames(politics_agg)[2] <- "politics_morality"

polinterest_agg <- aggregate(polinterest_second$pos_moremo, list(polinterest_second$date), mean)
colnames(polinterest_agg)[1] <- "date"
colnames(polinterest_agg)[2] <- "polinterest_morality"

newspaper_tweets_agg <- newspaper_tweets_agg |>
  filter(date > "2017-09-24" & date < "2022-01-01")

politics_agg <- politics_agg |>
  filter(date > "2017-09-24" & date < "2022-01-01")

polinterest_agg <- polinterest_agg |>
  filter(date > "2017-09-24" & date < "2022-01-01")

# Create a sequence of dates from August 15, 2015 to January 01, 2017
date_sequence <- seq(from = as.Date("2017-09-24"), to = as.Date("2022-01-01"), by = "day")

# Create a dataframe using the date sequence
var_dataframe <- tibble(date = date_sequence)

var_dataframe <- merge(var_dataframe, newspaper_tweets_agg, all.x = TRUE)
var_dataframe <- merge(var_dataframe, politics_agg, all.x = TRUE)
var_dataframe <- merge(var_dataframe, polinterest_agg, all.x = TRUE)

#set NAs to 0
var_dataframe[is.na(var_dataframe)] <- 0
var_dataframe$date <- NULL

var <- VAR(var_dataframe, p = 1, type = "const")


# Open the PNG device
pdf("./figures/figureF5.pdf", width = 12, height = 4)

# Set up the layout to have 3 plots in a row
par(mfrow=c(1, 3))

# Plot 1: Series Newspaper
acf(residuals(var)[,1], main="Series Newspaper")

# Plot 2: Series Politics
acf(residuals(var)[,2], main="Series Politics")

# Plot 3: Series Public
acf(residuals(var)[,3], main="Series Public")

# Reset the par settings to default (optional, useful if you're continuing with other plots)
par(mfrow=c(1, 1))

# Close the device
dev.off()

### Figure F6 ---------------------------------------------

newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
newspaper_tweets_second <- newspaper_tweets |> 
  filter(date >= "2017-09-25")

politics_second <- politics |> 
  filter(date >= "2017-09-25")

polinterest$date <- as.Date(polinterest$created_at)
polinterest_second <- polinterest |> 
  filter(date >= "2017-09-25")

newspaper_tweets_agg <- aggregate(newspaper_tweets_second$neg_moremo, list(newspaper_tweets_second$date), mean)
colnames(newspaper_tweets_agg)[1] <- "date"
colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"

politics_agg <- aggregate(politics_second$neg_moremo, list(politics_second$date), mean)
colnames(politics_agg)[1] <- "date"
colnames(politics_agg)[2] <- "politics_morality"

polinterest_agg <- aggregate(polinterest_second$neg_moremo, list(polinterest_second$date), mean)
colnames(polinterest_agg)[1] <- "date"
colnames(polinterest_agg)[2] <- "polinterest_morality"

newspaper_tweets_agg <- newspaper_tweets_agg |>
  filter(date > "2017-09-24" & date < "2022-01-01")

politics_agg <- politics_agg |>
  filter(date > "2017-09-24" & date < "2022-01-01")

polinterest_agg <- polinterest_agg |>
  filter(date > "2017-09-24" & date < "2022-01-01")

# Create a sequence of dates from August 15, 2015 to January 01, 2017
date_sequence <- seq(from = as.Date("2017-09-24"), to = as.Date("2022-01-01"), by = "day")

# Create a dataframe using the date sequence
var_dataframe <- tibble(date = date_sequence)

var_dataframe <- merge(var_dataframe, newspaper_tweets_agg, all.x = TRUE)
var_dataframe <- merge(var_dataframe, politics_agg, all.x = TRUE)
var_dataframe <- merge(var_dataframe, polinterest_agg, all.x = TRUE)

#set NAs to 0
var_dataframe[is.na(var_dataframe)] <- 0
var_dataframe$date <- NULL

var <- VAR(var_dataframe, p = 1, type = "const")


# Open the PNG device
pdf("./figures/figureF6.pdf", width = 12, height = 4)

# Set up the layout to have 3 plots in a row
par(mfrow=c(1, 3))

# Plot 1: Series Newspaper
acf(residuals(var)[,1], main="Series Newspaper")

# Plot 2: Series Politics
acf(residuals(var)[,2], main="Series Politics")

# Plot 3: Series Public
acf(residuals(var)[,3], main="Series Public")

# Reset the par settings to default (optional, useful if you're continuing with other plots)
par(mfrow=c(1, 1))

# Close the device
dev.off()

### Figure F7 --------------------------------------------------

newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
newspaper_tweets_second <- newspaper_tweets |> 
  filter(date >= "2017-09-25")

politics_second <- politics |> 
  filter(date >= "2017-09-25")

polinterest$date <- as.Date(polinterest$created_at)
polinterest_second <- polinterest |> 
  filter(date >= "2017-09-25")

partytype_agg <- aggregate(politics_second$morality_only_pos, list(politics_second$date, politics_second$partytype), mean)
colnames(partytype_agg)[1] <- "date"
colnames(partytype_agg)[2] <- "partytype"
partytype_wide <- partytype_agg %>% spread(partytype, x)

newspaper_tweets_agg <- aggregate(newspaper_tweets_second$morality_only_pos, list(newspaper_tweets_second$date), mean)
colnames(newspaper_tweets_agg)[1] <- "date"
colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"

polinterest_agg <- aggregate(polinterest_second$morality_only_pos, list(polinterest_second$date), mean)
colnames(polinterest_agg)[1] <- "date"
colnames(polinterest_agg)[2] <- "polinterest_morality"

# Create a sequence of dates from August 15, 2015 to January 01, 2017
date_sequence <- seq(from = as.Date("2017-09-25"), to = as.Date("2022-01-01"), by = "day")

# Create a dataframe using the date sequence
var_complete <- tibble(date = date_sequence)

var_complete <- merge(var_complete, partytype_wide, all.x = TRUE)
var_complete <- merge(var_complete, newspaper_tweets_agg, all.x = TRUE)
var_complete <- merge(var_complete, polinterest_agg, all.x = TRUE)

#set NAs to 0
var_complete[is.na(var_complete)] <- 0
var_complete$date <- NULL

var <- VAR(var_complete, p = 1, type = "const")

# Open the PNG device
pdf("./figures/figureF7.pdf", width = 12, height = 4)

# Set up the layout to have 3 plots in a row
par(mfrow=c(2, 3))

# Plot 1: Series Newspaper
acf(residuals(var)[,1], main="Series Center Left")

# Plot 2: Series Politics
acf(residuals(var)[,2], main="Series Center Right")

# Plot 3: Series Public
acf(residuals(var)[,3], main="Series Radical Left")

# Plot 1: Series Newspaper
acf(residuals(var)[,4], main="Series Radical Right")

# Plot 2: Series Politics
acf(residuals(var)[,5], main="Series Newspaper")

# Plot 3: Series Public
acf(residuals(var)[,6], main="Series Public")

# Reset the par settings to default (optional, useful if you're continuing with other plots)
par(mfrow=c(1, 1))

# Close the device
dev.off()

### Figure F8 --------------------------------------------------

newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
newspaper_tweets_second <- newspaper_tweets |> 
  filter(date >= "2017-09-25")

politics_second <- politics |> 
  filter(date >= "2017-09-25")

polinterest$date <- as.Date(polinterest$created_at)
polinterest_second <- polinterest |> 
  filter(date >= "2017-09-25")

partytype_agg <- aggregate(politics_second$morality_only_neg, list(politics_second$date, politics_second$partytype), mean)
colnames(partytype_agg)[1] <- "date"
colnames(partytype_agg)[2] <- "partytype"
partytype_wide <- partytype_agg %>% spread(partytype, x)

newspaper_tweets_agg <- aggregate(newspaper_tweets_second$morality_only_neg, list(newspaper_tweets_second$date), mean)
colnames(newspaper_tweets_agg)[1] <- "date"
colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"

polinterest_agg <- aggregate(polinterest_second$morality_only_neg, list(polinterest_second$date), mean)
colnames(polinterest_agg)[1] <- "date"
colnames(polinterest_agg)[2] <- "polinterest_morality"

# Create a sequence of dates from August 15, 2015 to January 01, 2017
date_sequence <- seq(from = as.Date("2017-09-25"), to = as.Date("2022-01-01"), by = "day")

# Create a dataframe using the date sequence
var_complete <- tibble(date = date_sequence)

var_complete <- merge(var_complete, partytype_wide, all.x = TRUE)
var_complete <- merge(var_complete, newspaper_tweets_agg, all.x = TRUE)
var_complete <- merge(var_complete, polinterest_agg, all.x = TRUE)

#set NAs to 0
var_complete[is.na(var_complete)] <- 0
var_complete$date <- NULL

var <- VAR(var_complete, p = 1, type = "const")

# Open the PNG device
pdf("./figures/figureF8.pdf", width = 12, height = 4)

# Set up the layout to have 3 plots in a row
par(mfrow=c(2, 3))

# Plot 1: Series Newspaper
acf(residuals(var)[,1], main="Series Center Left")

# Plot 2: Series Politics
acf(residuals(var)[,2], main="Series Center Right")

# Plot 3: Series Public
acf(residuals(var)[,3], main="Series Radical Left")

# Plot 1: Series Newspaper
acf(residuals(var)[,4], main="Series Radical Right")

# Plot 2: Series Politics
acf(residuals(var)[,5], main="Series Newspaper")

# Plot 3: Series Public
acf(residuals(var)[,6], main="Series Public")

# Reset the par settings to default (optional, useful if you're continuing with other plots)
par(mfrow=c(1, 1))

# Close the device
dev.off()

### Figure F9 --------------------------------------------------

newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
newspaper_tweets_second <- newspaper_tweets |> 
  filter(date >= "2017-09-25")

politics_second <- politics |> 
  filter(date >= "2017-09-25")

polinterest$date <- as.Date(polinterest$created_at)
polinterest_second <- polinterest |> 
  filter(date >= "2017-09-25")

partytype_agg <- aggregate(politics_second$pos_moremo, list(politics_second$date, politics_second$partytype), mean)
colnames(partytype_agg)[1] <- "date"
colnames(partytype_agg)[2] <- "partytype"
partytype_wide <- partytype_agg %>% spread(partytype, x)

newspaper_tweets_agg <- aggregate(newspaper_tweets_second$pos_moremo, list(newspaper_tweets_second$date), mean)
colnames(newspaper_tweets_agg)[1] <- "date"
colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"

polinterest_agg <- aggregate(polinterest_second$pos_moremo, list(polinterest_second$date), mean)
colnames(polinterest_agg)[1] <- "date"
colnames(polinterest_agg)[2] <- "polinterest_morality"

# Create a sequence of dates from August 15, 2015 to January 01, 2017
date_sequence <- seq(from = as.Date("2017-09-25"), to = as.Date("2022-01-01"), by = "day")

# Create a dataframe using the date sequence
var_complete <- tibble(date = date_sequence)

var_complete <- merge(var_complete, partytype_wide, all.x = TRUE)
var_complete <- merge(var_complete, newspaper_tweets_agg, all.x = TRUE)
var_complete <- merge(var_complete, polinterest_agg, all.x = TRUE)

#set NAs to 0
var_complete[is.na(var_complete)] <- 0
var_complete$date <- NULL

var <- VAR(var_complete, p = 1, type = "const")

# Open the PNG device
pdf("./figures/figureF9.pdf", width = 12, height = 4)

# Set up the layout to have 3 plots in a row
par(mfrow=c(2, 3))

# Plot 1: Series Newspaper
acf(residuals(var)[,1], main="Series Center Left")

# Plot 2: Series Politics
acf(residuals(var)[,2], main="Series Center Right")

# Plot 3: Series Public
acf(residuals(var)[,3], main="Series Radical Left")

# Plot 1: Series Newspaper
acf(residuals(var)[,4], main="Series Radical Right")

# Plot 2: Series Politics
acf(residuals(var)[,5], main="Series Newspaper")

# Plot 3: Series Public
acf(residuals(var)[,6], main="Series Public")

# Reset the par settings to default (optional, useful if you're continuing with other plots)
par(mfrow=c(1, 1))

# Close the device
dev.off()

### Figure F10 --------------------------------------------------

newspaper_tweets$date <- as.Date(newspaper_tweets$created_at)
newspaper_tweets_second <- newspaper_tweets |> 
  filter(date >= "2017-09-25")

politics_second <- politics |> 
  filter(date >= "2017-09-25")

polinterest$date <- as.Date(polinterest$created_at)
polinterest_second <- polinterest |> 
  filter(date >= "2017-09-25")

partytype_agg <- aggregate(politics_second$neg_moremo, list(politics_second$date, politics_second$partytype), mean)
colnames(partytype_agg)[1] <- "date"
colnames(partytype_agg)[2] <- "partytype"
partytype_wide <- partytype_agg %>% spread(partytype, x)

newspaper_tweets_agg <- aggregate(newspaper_tweets_second$neg_moremo, list(newspaper_tweets_second$date), mean)
colnames(newspaper_tweets_agg)[1] <- "date"
colnames(newspaper_tweets_agg)[2] <- "newspaper_tweets_morality"

polinterest_agg <- aggregate(polinterest_second$neg_moremo, list(polinterest_second$date), mean)
colnames(polinterest_agg)[1] <- "date"
colnames(polinterest_agg)[2] <- "polinterest_morality"

# Create a sequence of dates from August 15, 2015 to January 01, 2017
date_sequence <- seq(from = as.Date("2017-09-25"), to = as.Date("2022-01-01"), by = "day")

# Create a dataframe using the date sequence
var_complete <- tibble(date = date_sequence)

var_complete <- merge(var_complete, partytype_wide, all.x = TRUE)
var_complete <- merge(var_complete, newspaper_tweets_agg, all.x = TRUE)
var_complete <- merge(var_complete, polinterest_agg, all.x = TRUE)

#set NAs to 0
var_complete[is.na(var_complete)] <- 0
var_complete$date <- NULL

var <- VAR(var_complete, p = 1, type = "const")

# Open the PNG device
pdf("./figures/figureF10.pdf", width = 12, height = 4)

# Set up the layout to have 3 plots in a row
par(mfrow=c(2, 3))

# Plot 1: Series Newspaper
acf(residuals(var)[,1], main="Series Center Left")

# Plot 2: Series Politics
acf(residuals(var)[,2], main="Series Center Right")

# Plot 3: Series Public
acf(residuals(var)[,3], main="Series Radical Left")

# Plot 1: Series Newspaper
acf(residuals(var)[,4], main="Series Radical Right")

# Plot 2: Series Politics
acf(residuals(var)[,5], main="Series Newspaper")

# Plot 3: Series Public
acf(residuals(var)[,6], main="Series Public")

# Reset the par settings to default (optional, useful if you're continuing with other plots)
par(mfrow=c(1, 1))

# Close the device
dev.off()


### Figure G1 ---------------------------------------

emotional_appeals <- c("morality_only_pos", "morality_only_neg", "pos_moremo", "neg_moremo")

complex_complete <- NULL
results <- svar_complex_complete(emotional_appeals, 1)
create_coef_plot_complex_complete(results)

ggsave("./figures/figureG1.pdf", width=12, height=8)


### Figure G2 ----------------------------------------

emotional_appeals <- c("morality_only_pos", "morality_only_neg", "pos_moremo", "neg_moremo")

complex_full <- NULL
results <- svar_full(emotional_appeals, 1)
create_coef_plot_complex_full(results)

ggsave("./figures/figureG2.pdf", width=12, height=8)


### Figure H1 ----------------------------------------


results1_svar <- svar_simple_withoutBild("morality_only_pos", 1)

coef_plot_svar1 <- create_coef_plot_simple(results1_svar)

results2_svar <- svar_simple_withoutBild("morality_only_neg", 1)

coef_plot_svar2 <- create_coef_plot_simple(results2_svar)

results3_svar <- svar_simple_withoutBild("pos_moremo", 1)

coef_plot_svar3 <- create_coef_plot_simple(results3_svar)

results4_svar <- svar_simple_withoutBild("neg_moremo", 1)

coef_plot_svar4 <- create_coef_plot_simple(results4_svar)

# Combine the two plots
create_coef_plot_simple2(results1_svar, results2_svar, results3_svar, results4_svar)
ggsave("./figures/figureH1.pdf", width=12, height=8)


### Figure H2 ----------------------------------------
emotional_appeals <- c("morality_only_pos", "morality_only_neg", "pos_moremo", "neg_moremo")

complex_complete <- NULL
results <- svar_complex_complete_binary_withoutBild(emotional_appeals, 1)
create_coef_plot_complex_complete_binary(results)
ggsave("./figures/figureH2.pdf", width=12, height=8)




